import { _decorator, Component, Node, EventTouch, input, Input, Vec3, Vec2, Prefab, instantiate, Canvas, UITransform } from 'cc';
import { GravityWell } from './GravityWell';
const { ccclass, property } = _decorator;

@ccclass('TouchController')
export class TouchController extends Component {
    
    @property(Prefab)
    gravityWellPrefab: Prefab = null!;  // 引力井预制体
    
    @property(Node)
    gravityWellsParent: Node = null!;  // 引力井容器
    
    @property
    maxActiveWells: number = 3;  // 最多同时存在的引力井数量
    
    @property
    wellLifetime: number = 20;  // 引力井存在时间（秒）
    
    private activeWells: Node[] = [];  // 当前活跃的引力井
    private currentWell: Node | null = null; // 当前拖拽的引力井
    
    onLoad() {
        // 在节点上监听触摸事件，而不是全局输入
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }
    
    onDestroy() {
        // 移除事件监听
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }
    
    onTouchStart(event: EventTouch) {
        // 获取触摸在节点坐标系中的位置
        const touchPos = event.getUILocation();
        const worldPos = this.node.getComponent(UITransform)?.convertToNodeSpaceAR(new Vec3(touchPos.x, touchPos.y, 0)) || Vec3.ZERO;
        
        // 创建新的引力井
        this.createGravityWell(worldPos);
    }
    
    onTouchMove(event: EventTouch) {
        // 如果有当前拖拽的引力井，更新位置
        if (this.currentWell) {
            const touchPos = event.getUILocation();
            const worldPos = this.node.getComponent(UITransform)?.convertToNodeSpaceAR(new Vec3(touchPos.x, touchPos.y, 0)) || Vec3.ZERO;
            this.currentWell.setPosition(worldPos);
        }
    }
    
    onTouchEnd(event: EventTouch) {
        // 触摸结束，清除当前拖拽的引力井引用
        this.currentWell = null;
    }
    
    // 创建引力井
    createGravityWell(position: Vec3) {
        if (!this.gravityWellPrefab || !this.gravityWellsParent) return;
        
        // 如果超过最大数量，移除最老的引力井
        if (this.activeWells.length >= this.maxActiveWells) {
            const oldestWell = this.activeWells.shift();
            if (oldestWell) {
                const wellComponent = oldestWell.getComponent(GravityWell);
                if (wellComponent) {
                    wellComponent.deactivate();
                } else {
                    oldestWell.destroy();
                }
            }
        }
        
        // 创建新的引力井
        const newWell = instantiate(this.gravityWellPrefab);
        newWell.setPosition(position);
        
        // 添加到场景和管理数组
        this.gravityWellsParent.addChild(newWell);
        this.activeWells.push(newWell);
        this.currentWell = newWell; // 设置为当前拖拽对象
        
        // 激活效果
        const wellComponent = newWell.getComponent(GravityWell);
        if (wellComponent) {
            wellComponent.activate();
        }
        
        // 设置自动销毁
        this.scheduleOnce(() => {
            this.removeWell(newWell);
        }, this.wellLifetime);
    }
    
    // 移除指定的引力井
    removeWell(well: Node) {
        const index = this.activeWells.indexOf(well);
        if (index >= 0) {
            this.activeWells.splice(index, 1);
            
            // 如果正在拖拽这个引力井，清除引用
            if (this.currentWell === well) {
                this.currentWell = null;
            }
            
            const wellComponent = well.getComponent(GravityWell);
            if (wellComponent) {
                wellComponent.deactivate();
            } else {
                well.destroy();
            }
        }
    }
}